
package com.eazy.lkv2.web.controller.mobile.hotel;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.eazy.lkv2.web.controller.BaseController;
import com.eazy.lkv2.web.entity.Hotel;
import com.eazy.lkv2.web.entity.RoomImage;
import com.eazy.lkv2.web.service.hotel.HotelCommentService;
import com.eazy.lkv2.web.service.hotel.HotelService;
import com.eazy.lkv2.web.service.hotel.RoomImgService;
import com.eazy.lkv2.web.service.hotel.RoomService;

@Controller
public class MobileHotelController extends BaseController<Hotel, Integer> {
	
	@Autowired
	private HotelService hotelService;
	@Autowired
	private HotelCommentService hotelCommentService;
	@Autowired
	private RoomService roomService;
	@Autowired
	private RoomImgService roomImgService;
	
	/**
	 * 按城市查找酒店、暂时不考虑入住和离店时间的查询条件
	 */
	@RequestMapping(value = "hotellist.html", method = RequestMethod.GET)
	public String hotelList(Integer cityID,String checkInDate,String checkOutDate,Model model) {
		// 入住日期，离店日期放session里
		setAttrSession("checkInDate", checkInDate);
		setAttrSession("checkOutDate", checkOutDate);
		
		List<Map<String, Object>> data = null;
		
		if(cityID == 0) {
			data = hotelService.select();
		} else {
			data = hotelService.selectByCityID(cityID);
		}
		for(Map<String, Object> k : data) {
			List<Map<String, Object>> comment = hotelCommentService.selectCommentByHotelId(k.get("id").toString());
			if(comment.isEmpty() || comment == null) {
				k.put("count", 0);
			} else {
				k.put("count", comment.size());
			}
		}
		// 实现一个规则，一般都是评论数多的在前面那么此处排下序
		Collections.sort(data,new Comparator<Map<String, Object>>() {

			@Override
			public int compare(Map<String, Object> o1, Map<String, Object> o2) {
				return (o2.get("count") + "").compareTo((o1.get("count") + ""));
			}
		});
		model.addAttribute("data", data);
		return getDefault("hotel/hotellist");
	}
	
	@RequestMapping(value = "hotel.html/{hotelId}", method = RequestMethod.GET)
	public String hotel(@PathVariable String hotelId,Model model) {
		String checkInDate = getAttrSessionToStr("checkInDate");
		String checkOutDate = getAttrSessionToStr("checkOutDate");
		
		// 这边图片太多了，只能减少代码执行时间，来提高图片加载速度
		
		// 拿到酒店信息
		Hotel hotel = findById(parseInt(hotelId));
		
		// 酒店房间集合
		List<Map<String, Object>> roomCollection = null;
		
		if(existsCatch("roomCollection")) {
			String catchData = getCatch("roomCollection");
			roomCollection = toGson(catchData);
		} else {
			roomCollection = roomService.selectRoomById(hotelId);
			for(Map<String, Object> r : roomCollection) {
				// 拿到房间的子集
				List<RoomImage> roomImgColl = roomImgService.queryByObject(new RoomImage(parseInt(r.get("id").toString())));
				r.put("roomImgColl", roomImgColl);
			}
			String catchData = toJson(roomCollection);
			putCatch("leftMenu", catchData , 60 * 30); // 30分钟后重置数据
		}
		
		model.addAttribute("hotel", hotel);
		model.addAttribute("roomCollection", roomCollection);
		model.addAttribute("checkInDate", checkInDate);
		model.addAttribute("checkOutDate", checkOutDate);
		return getDefault("hotel/hotel");
	}
	
	@RequestMapping(value = "hotelinfo.html/{hotelId}", method = RequestMethod.GET)
	public String hotelInfo(@PathVariable String hotelId,Model model) {
		Hotel hotel = findById(parseInt(hotelId));
		model.addAttribute("hotel", hotel);
		
		return getDefault("hotel/hotelinfo");
	}
}